package org.solon.demo.controller;

import org.noear.solon.annotation.Body;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Delete;
import org.noear.solon.annotation.Get;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.annotation.Param;
import org.noear.solon.annotation.Post;
import org.noear.solon.annotation.Put;
import org.noear.solon.core.handle.Result;
import org.noear.solon.core.handle.UploadedFile;
import org.solon.demo.controller.param.UserAddParam;
import org.solon.demo.controller.param.UserUpdateParam;
import org.solon.demo.controller.vo.OrderVO;
import org.solon.demo.controller.vo.TreeVO;
import org.solon.demo.controller.vo.UserVO;


/**
 * 用户模块
 * @author thc
 */
@Controller
@Mapping("user")
public class UserController {

    /**
     * 添加用户1
     * @apiNote 根据表单内容新建用户
     * @param userAddParam
     * @return
     */
    @Post
    @Mapping("add")
    public Result<Long> add(@Body UserAddParam userAddParam) {
        System.out.println(userAddParam);
        return Result.succeed(System.currentTimeMillis());
    }

    /**
     * 修改用户
     * @apiNote 根据表单内容修改用户
     * @param userUpdateParam
     * @return
     */
    @Put
    @Mapping("update")
    public Result<Long> add(@Body UserUpdateParam userUpdateParam) {
        System.out.println(userUpdateParam);
        return Result.succeed(System.currentTimeMillis());
    }

    /**
     * 获取用户
     * @param userId 用户id|111
     * @return
     */
    @Get
    @Mapping("get")
    public Result<UserVO> get(Long userId) {
        UserVO userVO = new UserVO();
        userVO.setUserId(userId);
        userVO.setUsername("Jim");
        userVO.setEmail("jim@aa.com");
        return Result.succeed(userVO);
    }


    /**
     * 删除用户
     * @param userId 用户id|111
     * @return
     */
    @Delete
    @Mapping("delete")
    public Result<UserVO> delete(@Param(required = true) Long userId) {
        UserVO userVO = new UserVO();
        userVO.setUserId(userId);
        userVO.setUsername("Jim");
        userVO.setEmail("jim@aa.com");
        return Result.succeed(userVO);
    }

    /**
     * 上传文件
     * @param file 文件
     * @return
     */
    @Post
    @Mapping("upload")
    public Result<String> upload(UploadedFile file) {
        System.out.println(file.getName());
        return Result.succeed("http://aaa.com/" + file.getName());
    }

    /**
     * 树状返回
     * @return
     */
    @Get
    @Mapping("tree")
    public Result<TreeVO> tree() {
        return Result.succeed(new TreeVO());
    }

    /**
     * 获取订单详情
     * @apiNote 演示嵌套对象
     * @param orderNo 订单编码|77777
     * @return
     */
    @Get
    @Mapping("order")
    public Result<OrderVO> tree(@Param(required = true) String orderNo) {
        return Result.succeed(new OrderVO());
    }


}
